package org.robot.utils;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class PageUtil {
	private static Log log = LogFactory.getLog(PageUtil.class);

	/**
	 * 如果的destFile存在，直接读取，否则下载网页并保存在本地
	 * @author pdz
	 * @date 2018年12月20日
	 * @param url
	 * @param destFile 调用FileUtil类里面的方法
	 * @return
	 * @throws Exception 如果返回状态码小于200大于等于400，抛出异常
	 */
	public static Document downDoc(String url, File destFile) {
		try(CloseableHttpClient client = HttpClients.custom().setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36").build();) {
			if (destFile.exists()) {
				return Jsoup.parse(destFile, "utf-8");
			} else {
				HttpGet httpGet = new HttpGet(url);
				CloseableHttpResponse response = client.execute(httpGet);
				int statusCode = response.getStatusLine().getStatusCode();
				if (statusCode < 200 || statusCode >= 400) {
					throw new HttpStatusException("http错误", statusCode, url);
				}
				HttpEntity entity = response.getEntity();
				String html = EntityUtils.toString(entity, "utf-8");
				Document doc = Jsoup.parse(html, url);
				// 所有链接转成绝对链接
				Elements aEles = doc.select("a[href]");
				aEles.forEach(aEle -> aEle.attr("href", aEle.absUrl("href")));
				FileUtils.writeStringToFile(destFile, doc.html(), "utf-8");
				log.info("网络读取：" + url + " 并保存到本地：" + destFile.getAbsolutePath());
				return doc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Document downDoc2(String url, File destFile) {
		try(CloseableHttpClient client = HttpClients.custom().setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36").build();) {
			if (destFile.exists()) {
				return Jsoup.parse(destFile, "utf-8");
			} else {
				HttpGet httpGet = new HttpGet(url);
				CloseableHttpResponse response = client.execute(httpGet);
				int statusCode = response.getStatusLine().getStatusCode();
				if (statusCode == 200 || statusCode >= 400) {
					throw new HttpStatusException("http错误", statusCode, url);
				}
				HttpEntity entity = response.getEntity();
				String html = EntityUtils.toString(entity, "utf-8");
				Document doc = Jsoup.parse(html, url);
				// 所有链接转成绝对链接
				Elements aEles = doc.select("a[href]");
				aEles.forEach(aEle -> aEle.attr("href", aEle.absUrl("href")));
				FileUtils.writeStringToFile(destFile, doc.html(), "utf-8");
				log.info("网络读取：" + url + " 并保存到本地：" + destFile.getAbsolutePath());
				return doc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Document downDoc1(String url, File destFile) throws Exception {
		if (destFile.exists()) {
			return Jsoup.parse(destFile, "utf-8");
		} else {
			CloseableHttpClient client = HttpClients.custom().setUserAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36").build();
			HttpGet httpGet = new HttpGet(url);
			CloseableHttpResponse response = client.execute(httpGet);
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode < 200 || statusCode >= 400) {
				throw new HttpStatusException("http错误", statusCode, url);
			}
			HttpEntity entity = response.getEntity();
			String html = EntityUtils.toString(entity, "utf-8");
			Document doc = Jsoup.parse(html, url);
			// 所有链接转成绝对链接
			Elements aEles = doc.select("a[href]");
			aEles.forEach(aEle -> aEle.attr("href", aEle.absUrl("href")));
			FileUtils.writeStringToFile(destFile, doc.html(), "utf-8");
			log.info("网络读取：" + url + " 并保存到本地：" + destFile.getAbsolutePath());
			return doc;
		}
	}
	
	public static void main(String[] args) {
		ts();
	}

	
	private static Document ts2() {
		try {
			return ts1();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Jsoup.parse("<a>haha</a>");
	}

	private static Document ts1() throws IOException {
		//E:/webSpider/Bioone/seed/A000A0/B846E191990673C2051EA2E35D7CBBC4.html
		Document doc = Jsoup.parse(new File("E:/webSpider/Bioone/seed/A000/B846E191990673C2051EA2E35D7CBBC4.html"), "utf-8");
		return doc;
	}

	private static void ts() {
		String url = "http://manu39.magtech.com.cn/Jwk_gtlxxb/CN/volumn/volumn_1223.shtml";
		try {
			Document doc = PageUtil.downDoc2(url, FileUtil.destFile(url, "haha", "e:/webSpider/Sagepub"));
			doc.select("a[href]").eachAttr("href").forEach(System.out::println);
			System.out.println(doc.text());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
